package com.djsearch.data;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class GetProviceAndCityData {

	private List<Province> list = new ArrayList<Province>();
	public List<Province> StartToGetProvinceList(){

        Thread thread = new Thread() {
                @Override
                public void run() {
                    String url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";
                    String xmlString = HttpRequestManager.getInstance().
                    getStringFromServer(url, 50000);
                    try {
                    	ByteArrayInputStream tInputStringStream = 
                        	new ByteArrayInputStream(xmlString.getBytes());
                        XmlPullParser parser = Xml.newPullParser();
                        parser.setInput(tInputStringStream, "utf-8");
                        int types = parser.getEventType();
                        while (types != XmlPullParser.END_DOCUMENT) {
                                switch (types) {
                                case XmlPullParser.START_DOCUMENT:
                                        break;
                                case XmlPullParser.START_TAG:
                                	if ("string".equals(parser.getName())) {
                                   	    String provinceStr = parser.nextText();
                                        Province province = new Province();
                                        int n = provinceStr.indexOf(',');
                                        province.name = provinceStr.substring(0, n);
                                        province.code = Long.valueOf(provinceStr.substring(n + 1));
                                        list.add(province);  
                                   }
                                   break;   
                                case XmlPullParser.END_TAG:
                                   break;

                                case XmlPullParser.END_DOCUMENT:
                                   break;
                                }
                                types = parser.next();
                        }
                        tInputStringStream.close();
                    } catch (XmlPullParserException e) {

                            e.printStackTrace();

                    } catch (IOException e) {

                            e.printStackTrace();

                    }
                }
        };
        thread.start();
        thread = null;
        return list;

	}

	public void SaveCity(final Province pro){

		
        final List<City> cityList = new ArrayList<City>();
        Thread thread = new Thread() {
                @Override
                public void run() {
                	String url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";
                    String xmlString = HttpRequestManager.getInstance().
                    getStringFromServer(url, 50000);
                    DefaultHttpClient client = new DefaultHttpClient();
                    HttpUriRequest req = new HttpGet(url);
                    HttpResponse res;
                    try {

                        res = client.execute(req);
                        HttpEntity entity = res.getEntity();
                        InputStream inputStream = entity.getContent();
                        XmlPullParser parser = Xml.newPullParser();
                        parser.setInput(inputStream, "utf-8");
                        int types = parser.getEventType();
                        while (types != XmlPullParser.END_DOCUMENT) {
                                switch (types) {
                                case XmlPullParser.START_DOCUMENT:
                                 break;
                                case XmlPullParser.START_TAG:
                                	 if ("string".equals(parser.getName())) {
                                         String cityStr = parser.nextText();
                                         int n = cityStr.indexOf(',');
                                         City city = new City();
                                         city.province = pro.name;
                                         city.provinceCode = pro.code;
                                         city.city = cityStr.substring(0, n);
                                         city.cityCode = Long.valueOf(cityStr
                                                         .substring(n + 1));
                                         cityList.add(city);
                                 }
                                 break;  
                                case XmlPullParser.END_TAG:
                                 break;
                                }
                                types = parser.next();

                        }
                        inputStream.close();
                
                    } catch (XmlPullParserException e) {

                            e.printStackTrace();

                    } catch (IOException e) {

                            e.printStackTrace();
                    }
                     
                    cityList.clear();   
                }

        };
        thread.start();
        thread = null;

	}

	
	
	public class Province {
		String name;//省份名字
		Long code;//省份编号
	}
	
	public class City{
        public String province;//省份名字
        public String city;//城市名字
        public long provinceCode;//省份代码
        public long cityCode;//城市代码
}

	
}
